/**
 * Created by sa on 2016/9/22.
 */

//var casper = require('casper').create();

//casper.start('http://wj.qq.com/', function() {
//  this.echo(this.getHTML());
//});

//casper.start('http://www.site.tld/', function() {
//  this.echo(this.getHTML('h1#foobar')); // => 'Plop'
//});

//
//casper.start('http://wj.qq.com/', function() {
//  this.echo(this.getTitle()); // "Google"
//});

//casper.start('https://www.google.com', function() {
//  this.echo("loaded");
//  this.reload(function() {
//    this.echo("loaded again");
//  });
//});

//var pathP = 'D:/work_nodeJS/crawl_youku_nodejs/casperjs/' + new Date().valueOf() + '.png'
//casper.start('http://www.google.com/', function() {
//  //if (this.resourceExists('logo3w.png')) {
//  if (this.resourceExists(pathP)) {
//    this.echo('Google logo loaded');
//  } else {
//    this.echo('Google logo not loaded', 'ERROR');
//  }
//});

//casper.start('https://www.baidu.com/', function() {
//  this.echo("I'm loaded.");
//});

//casper.start('http://www.baidu.com/', function() {
//  this.echo(this.status(true));
//});

/*

 casper.start();

 casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X)');

 casper.thenOpen('http://www.baidu.com/', function() {
 this.echo("I'm a Mac.");
 this.echo(this.status(true));
 this.userAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
 });

 casper.thenOpen('http://www.baidu.com/', function() {
 this.echo("I'm a PC.");
 this.echo(this.status(true));
 });
 */

/*

 casper.start('https://www.google.com/', function() {
 if (this.visible('#hplogo')) {
 this.echo("I can see the logo");
 } else {
 this.echo("I can't see the logo");
 }
 this.echo(this.getHTML());
 });
 */

var casper = require('casper').create({
  pageSettings: {
    loadImages: true,
    loadPlugins: false,
    userAgent: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 LBBROWSER'
  },
  logLevel: "debug",//日志等级
  verbose: true,    // 记录日志到控制台
  timeout: 60000,//60秒超时，退出
});

casper.start();


casper.viewport(1024, 1024);

var height = 0;
//var width = 0;

// Click the first link in the casperJS page
//casper.thenOpen('http://casperjs.org/' , function(){
casper.thenOpen('http://www.httpuseragent.org/', function () {
})


//casper.thenClick('a', function() {
//  this.echo("I clicked on first link found, the page is now loaded.");
//});

casper.then(function () {
  this.echo(this.getCurrentUrl());
});

casper.then(function () {

  //casper.viewport(width , height);

  var pathP = 'casperjs' + new Date().valueOf() + '.png'
  //this.capture(pathP, {
  //  top: 0,
  //  left: 0,
  //  width: 1024,
  //  height: height
  //});

  this.captureSelector(pathP, 'body');

  console.log("save img");

});

casper.run();



